package com.houtai.controller.user;


import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.ResultVo;
import com.houtai.bean.dto.user.*;
import com.houtai.bean.vo.file.FileVo;
import com.houtai.bean.vo.user.UserLoginVo;
import com.houtai.common.Contant;
import com.houtai.common.oss.OssUtil;
import com.houtai.common.util.CommonUtil;
import com.houtai.service.user.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@Api(value="用户接口", tags={"用户接口"})
@RequestMapping(value = {"api/v1"}, method = RequestMethod.POST)
public class UserController {

    @Autowired
    private UserService userService;


    @ApiOperation(value="发送验证码", notes="发送验证码")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/user/send/message")
    public ResultVo sendMessage (@ApiParam(value = "发送验证码所需参数") @Valid @RequestBody UserSendMessageDto dto, BindingResult result) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = userService.sendMessage(dto.getPhone_no());
        }
        return retInfo;
    }
    @ApiOperation(value="验证是否注册", notes="验证是否注册")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/user/is/register")
    public ResultVo isRegister (@ApiParam(value = "验证是否注册所需参数") @Valid @RequestBody UserSendMessageDto dto, BindingResult result) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = userService.isRegister(dto.getPhone_no());
        }
        return retInfo;
    }
    @ApiOperation(value="注册", notes="注册")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/user/register")
    public ResultVo register(@ApiParam(value = "注册所需参数") @Valid @RequestBody UserReisgerDto dto, BindingResult result) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = userService.register(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="手机登陆", notes="手机登陆")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/user/login/phone")
    public UserLoginVo phone_login(@ApiParam(value = "手机登陆所需参数") @Valid @RequestBody UserPhoneLoginDto dto, BindingResult result) {
        UserLoginVo retInfo = new UserLoginVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = userService.phone_login(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="密码登陆", notes="密码登陆")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/user/login/pass")
    public UserLoginVo pass_login(@ApiParam(value = "密码登陆所需参数") @Valid @RequestBody UserPassLoginDto dto, BindingResult result) {
        UserLoginVo retInfo = new UserLoginVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = userService.pass_login(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="修改密码", notes="修改密码")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/user/update/pass")
    public ResultVo update_pass(@ApiParam(value = "修改密码所需参数") @Valid @RequestBody UserUpdatePassDto dto, BindingResult result,
                               @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = userService.update_pass(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="忘记密码", notes="忘记密码")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/user/forget/pass")
    public ResultVo forget_pass(@ApiParam(value = "修改密码所需参数") @Valid @RequestBody UserForgetPassDto dto, BindingResult result) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = userService.forget_pass(dto);
        }
        return retInfo;
    }
    @Authority(value = AuthorityType.Login)
    @ApiOperation(value="修改头像(次接口为表单类型)", notes="修改头像")
    @RequestMapping(value = "/user/head/img", method = RequestMethod.POST)
    public FileVo file(@RequestParam(value = "frontFile", required = true) MultipartFile frontFile,
                       @RequestHeader String token)throws Exception{


        FileVo vo = new FileVo();
        if (CommonUtil.checkFileSize(frontFile.getSize(), 100,"K")) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmssS", Locale.CHINA);

            String frontFileName = frontFile.getOriginalFilename().substring(frontFile.getOriginalFilename().lastIndexOf(".") + 1);
            String imgName = formatter.format(new Date(System.currentTimeMillis()));
            String frontSavePath = Contant.FILE_IMG + "/user/head/" + imgName + "." + frontFileName;
            ObjectMetadata frontMetadata = new ObjectMetadata();

            InputStream inputStream = frontFile.getInputStream();
            frontMetadata.setContentLength(inputStream.available());
            OssUtil.uploadInputStream(inputStream, frontMetadata, frontSavePath);
            vo.setMark("0");
            vo.setTip("查询成功");
            vo.setFile_name(imgName + "." + frontFileName);
            vo.setFile_url(Contant.DOMAIN +  Contant.FILE_IMG + "/user/head/" + imgName + "." + frontFileName);
        } else {
            vo.setMark("1");
            vo.setTip("头像最大100Kb");
        }

        return vo;
    }
}
